package cn.xyf.mzmy.server.controller.websocket;

import cn.xyf.mzmy.common.netty.core.handler.TextWebSocketFrameHandler;
import cn.xyf.mzmy.common.pojo.Result;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@RestController
@RequestMapping("/nettyTest")
public class NettyTestController {

    // 发送消息给指定用户的端点
    @PostMapping("/sendToUser")
    public Result<String> sendMessageToUser(@RequestParam String userId, @RequestBody String messageContent) {
        try {
            // 构造要发送的消息
//            String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//            String fullMessage = String.format("{\"type\": \"notification\", \"content\": \"%s\", \"timestamp\": \"%s\"}", messageContent, timestamp);

            // 调用Netty处理器中的静态方法发送消息
            TextWebSocketFrameHandler.sendMessageToUser(userId, messageContent);

            return Result.ok("Message sent to user " + userId);
        } catch (Exception e) {
            throw new RuntimeException("运行异常");
        }
    }
}
